package com.lam.common.components.file;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lam.common.components.file.model.FileDownloadInfo;
import com.lam.common.components.file.model.FileBusiTypeEnum;
import com.lam.common.components.file.service.FileService;
import com.lam.common.config.CommonConfig;
import com.lam.common.utils.Base64Util;
import com.lam.common.utils.JsonUtil;
import com.lam.common.utils.Md5Util;
import com.lam.common.utils.SpringUtils;
import com.lam.common.utils.StringUtils;

public class FileDownloadUtil {

	private static final Logger logger = LoggerFactory.getLogger(FileDownloadUtil.class);

    private static final String KEY = "lam_yz_0BvqMiY9I4jsZes=";
    
    public static String createApiImgUrl(FileBusiTypeEnum busiType, String sign) {
    	return createApiImgUrl(getDownloadInfo(busiType, sign));
    }
    
    public static String createImgUrl(FileBusiTypeEnum busiType, String sign) {
    	return createImgUrl(getDownloadInfo(busiType, sign));
    }
    
    public static String createApiImgUrl(FileDownloadInfo info) {
		return info == null?null:String.format("%scommon/img?s=%s&t=%s", CommonConfig.me().getFrontPath(), info.getS(), info.getT());
	}
    
    public static String createImgUrl(FileDownloadInfo info) {
		return info == null?null:String.format("%scommon/img?s=%s&t=%s", CommonConfig.me().getAdminPath(), info.getS(), info.getT());
	}

    public static String encode(String path) {
        return Base64Util.encodeURLSafeString(path);
    }
    
    public static String getKeyMd5() {
        return Md5Util.md5Encrypt(KEY);
    }

    public static String sign(String path) {
        return StringUtils.sign(encode(path), KEY);
    }

    public static FileService getFileService() {
    	return SpringUtils.getBean(FileService.class);
    }
    
    public static FileDownloadInfo createDownloadInfo(FileBusiTypeEnum busiType, String path) {
    	return createDownloadInfo(busiType, path, -1);
    }
    
    public static FileDownloadInfo createDownloadInfo(FileBusiTypeEnum busiType, String path, int expireDay) {
    	return getFileService().createDownloadInfo(busiType, path, expireDay);
    }
    
    public static FileDownloadInfo getDownloadInfo(FileBusiTypeEnum busiType, String sign) {
    	return getFileService().getDownloadInfo(busiType, sign);
    }
    
    public static boolean deleteBySign(FileBusiTypeEnum busiType, String sign) {
    	return getFileService().deleteBySign(busiType, sign);
    }
    
    public static boolean deleteByPath(FileBusiTypeEnum busiType, String sign) {
    	return getFileService().deleteByPath(busiType, sign);
    }

    public static void writeFail(HttpServletResponse response, Exception e, String s) {
        logger.error(e.getMessage(), e);
        // 重置response
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
//        response.setContentType("application/json;charset=UTF-8");
        Map<String, String> map = new HashMap<>();
        map.put("status", "1001");
        map.put("s", s);
        map.put("msg", "下载失败：" + e.getMessage());
        String msgFormat = "<html><head></head><body><div></div><script>window.top.postMessage('%s', '*');</script></body></html>";
        try {
//            response.getWriter().println(JsonUtil.toJsonString(map));
            response.getWriter().println(String.format(msgFormat, JsonUtil.toJsonString(map)));
        } catch (IOException e1) {
            logger.error(e1.getMessage(), e1);
        }
    }
    
}
